﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 药品信息管理
{
    public partial class YP_ZDWH : Form
    {
        SqlDataAdapter da = new SqlDataAdapter();
        int currentLX=-1;
        int FLAG=-1;
        public YP_ZDWH()
        {
            InitializeComponent();
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if ((ActiveControl is TextBox || ActiveControl is ComboBox) && keyData == Keys.Enter)
                keyData = Keys.Tab;
            return base.ProcessDialogKey(keyData);
        }

        #region 显示药品
        private void YP_show(string TJ)
        {
            string flagTJ="";
            switch (comboBoxItem1.SelectedIndex)
            {
                case 0: 
                    //不显示停用药品
                    {
                        FLAG = 3;
                        flagTJ="SELECT * FROM ZD_YP WHERE FLAG=3 ";
                    } break;
                case 1:
                    //全部显示
                    {
                        FLAG = -1;
                        flagTJ = "SELECT * FROM ZD_YP WHERE 1=1";
                    } break;
                case 2:
                    //只显示停用药品
                    {
                        FLAG = 0;
                        flagTJ = "SELECT * FROM ZD_YP WHERE FLAG=0 ";
                    } break;

            }
            using (SqlConnection con = new SqlConnection(SQL.connectDB))
            {
                DataTable dt_yp = new DataTable();
                SqlCommand sc = new SqlCommand(flagTJ+TJ, con);
                da.SelectCommand = sc;
                da.Fill(dt_yp);
                dataGridViewX1.DataSource = dt_yp;
                SUMYP.Text = "药品数量：" + dt_yp.Rows.Count.ToString();
                dataGridViewX1.Columns[0].Visible = false;
                dataGridViewX1.Columns[1].HeaderText = "编码";
                dataGridViewX1.Columns[2].HeaderText = "拼音缩写";
                dataGridViewX1.Columns[3].HeaderText = "名称";
                //dataGridViewX1.Columns[0].HeaderText = "类型";
                //dataGridViewX1.Columns[0].HeaderText = "类型";
                //dataGridViewX1.Columns[0].HeaderText = "类型";

                //dataGridViewX1.Columns[0].HeaderText = "类型";
                //dataGridViewX1.Columns[0].HeaderText = "类型";
                //dataGridViewX1.Columns[0].HeaderText = "类型";
                //dataGridViewX1.Columns[0].HeaderText = "类型";
            }
        }
        #endregion

        private void YP_ZDWH_Load(object sender, EventArgs e)
        {
            try
            {
                #region 药品级别初始化
                using (SqlConnection con = new SqlConnection(SQL.connectDB))
                {
                    DataTable dt_jb = new DataTable();
                    SqlCommand JB = new SqlCommand("SELECT * FROM ZD_YPJB", con);//sql语句
                    da.SelectCommand = JB;//设置为已实例化SqlDataAdapter的查询命令
                    da.Fill(dt_jb);
                    for (int i = 0; i < dt_jb.Rows.Count; i++)
                    {
                        DevComponents.AdvTree.Node node = new DevComponents.AdvTree.Node();
                        node.Text = dt_jb.Rows[i][1].ToString();
                        node1.Nodes.Add(node);
                    }
                }
                #endregion

                comboBoxItem1.SelectedIndex = 0;

                YP_show("");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        #region 增加
        private void buttonItem1_Click(object sender, EventArgs e)
        {
            dataGridViewX1.CurrentCell = this.dataGridViewX1.Rows[this.dataGridViewX1.Rows.Count - 1].Cells[1];
            if(cmb_JB.Items.Count==0)
            {
                using (SqlConnection con = new SqlConnection(SQL.connectDB))
                {
                    DataTable dt_jb = new DataTable();
                    SqlCommand JB = new SqlCommand("SELECT * FROM ZD_YPJB", con);//sql语句
                    da.SelectCommand = JB;//设置为已实例化SqlDataAdapter的查询命令
                    da.Fill(dt_jb);
                    for (int i = 0; i < dt_jb.Rows.Count; i++)
                    {
                        cmb_JB.Items.Add(dt_jb.Rows[i][1].ToString());
                    }
                }
            }
        }
        #endregion

        #region 删除
        private void buttonItem2_Click(object sender, EventArgs e)
        {
            try
            {
                dataGridViewX1.Rows.Remove(dataGridViewX1.Rows[dataGridViewX1.CurrentCell.RowIndex]);
            }
            catch { }
        }
        #endregion

        #region 编辑
        private void buttonItem3_Click(object sender, EventArgs e)
        {
            //if (ds.Tables[1] != null)
            //{
            //    try
            //    {
            //        using (SqlConnection con = new SqlConnection(SQL.connectDB))
            //        {
            //            SqlDataAdapter uda = new SqlDataAdapter("SELECT * FROM ZD_YP", con);
            //            SqlCommandBuilder scb = new SqlCommandBuilder(uda);
            //            uda.Update(ds.Tables[1]);
            //        }
            //    }
            //    catch (Exception ex)
            //    {
            //        MessageBox.Show(ex.ToString());
            //    }
            //}
            //MessageBox.Show("保存成功！", "系统提示");
        }
        #endregion

      
        #region 显示西药
        private void XY_Click(object sender, EventArgs e)
        {
            YP_show("and LX=0");
            currentLX=0;
        }
        #endregion

        #region 显示全部药品
        private void superTabItem1_Click(object sender, EventArgs e)
        {
            YP_show("");
            currentLX = -1;
        }
        #endregion

        #region 显示成药
        private void CY_Click(object sender, EventArgs e)
        {
            YP_show("and LX=1");
            currentLX=1;
        }
        #endregion

        #region 显示草药
        private void ZCY_Click(object sender, EventArgs e)
        {
            YP_show("and LX=2");
            currentLX=2;
        }
        #endregion

        #region 编码定位
        private void textBoxItem1_InputTextChanged(object sender)
        {
            for (int i = 0; i < dataGridViewX1.RowCount-1;i++ )
            {
                if (dataGridViewX1.Rows[i].Cells[1].Value.ToString().Trim() == textBoxItem1.Text.ToString().Trim() || dataGridViewX1.Rows[i].Cells[2].Value.ToString().ToLower().Trim() == textBoxItem1.Text.ToString().Trim() || dataGridViewX1.Rows[i].Cells[2].Value.ToString().ToUpper().Trim() == textBoxItem1.Text.ToString().Trim())
                {
                    dataGridViewX1.Rows[i].Selected = true;
                    dataGridViewX1.FirstDisplayedScrollingRowIndex = i;
                }
                else
                {
                    dataGridViewX1.Rows[i].Selected = false;
                }
            }

        }
        #endregion

        #region 药品级别筛选
        private void advTree1_NodeClick(object sender, DevComponents.AdvTree.TreeNodeMouseEventArgs e)
        {
            string currentTJ;
            if (currentLX == -1)
                currentTJ = "";
            else currentTJ = " and LX=" + currentLX.ToString();
            using (SqlConnection con = new SqlConnection(SQL.connectDB))
            {
                DataTable dt_ypjb = new DataTable();
                SqlCommand sc = new SqlCommand("select * from ZD_YP where JB IN(select BM from ZD_YPJB where MC='" + advTree1.SelectedNode.ToString()+"')"+currentTJ, con);
                da.SelectCommand = sc;
                da.Fill(dt_ypjb);
                dataGridViewX1.DataSource = dt_ypjb;
                SUMYP.Text = "药品数量：" + dt_ypjb.Rows.Count.ToString();
                dataGridViewX1.Columns[0].Visible = false;
                dataGridViewX1.Columns[1].HeaderText = "编码";
                dataGridViewX1.Columns[2].HeaderText = "拼音缩写";
                dataGridViewX1.Columns[3].HeaderText = "名称";
                //dataGridViewX1.Columns[0].HeaderText = "类型";
                //dataGridViewX1.Columns[0].HeaderText = "类型";
                //dataGridViewX1.Columns[0].HeaderText = "类型";

                //dataGridViewX1.Columns[0].HeaderText = "类型";
                //dataGridViewX1.Columns[0].HeaderText = "类型";
                //dataGridViewX1.Columns[0].HeaderText = "类型";
                //dataGridViewX1.Columns[0].HeaderText = "类型";
            }
        }
        #endregion

        private void cmb_LX_TextChanged(object sender, EventArgs e)
        {
            if (cmb_LX.Text == "1")
                cmb_LX.SelectedIndex = 0;
            if (cmb_LX.Text == "2")
                cmb_LX.SelectedIndex = 1;
            if (cmb_LX.Text == "3")
                cmb_LX.SelectedIndex = 2;
        }

        private void cmb_FLAG_TextChanged(object sender, EventArgs e)
        {
            if (cmb_FLAG.Text == "1")
                cmb_FLAG.SelectedIndex = 0;
            if (cmb_FLAG.Text == "2")
                cmb_FLAG.SelectedIndex = 1;
        }

        private void dataGridViewX1_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                cmb_LX.SelectedIndex = Convert.ToInt16(dataGridViewX1.CurrentRow.Cells[0].Value);
                txt_BM.Text = dataGridViewX1.CurrentRow.Cells[1].Value.ToString();
                txt_PY.Text = dataGridViewX1.CurrentRow.Cells[2].Value.ToString();
            }
            catch { }
        }

        private void btn_Update_Click(object sender, EventArgs e)
        {
            using (SqlConnection con = new SqlConnection(SQL.connectDB))
            {
                con.Open();
                for (int i = 0; i < dataGridViewX1.RowCount - 1; i++)
                    if (dataGridViewX1.Rows[i].Cells[1].Value.ToString() == txt_BM.Text.ToString())
                    {
                        dataGridViewX1.Rows[i].Cells[2].Value = txt_PY.Text.ToString();
                        SqlCommand sc_up = new SqlCommand("UPDATE ZD_YP SET DH='" + txt_PY.Text.ToString() + "' WHERE BM='" + txt_BM.Text.ToString() + "'", con);
                        sc_up.ExecuteNonQuery();

                    }
                    else
                    {
                        //SqlCommand sc_in=new SqlCommand("INSERT INTO ZD_YP VALUES ("++")",con)
                        //sc_in.ExecuteNonQuery();
                        MessageBox.Show("");
                    }
            }
        }
    }
}